package hot_100;


public class AddTwoNumbers {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(0);
        ListNode cousor = res;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            int num1 = l1 != null ? l1.val : 0;
            int num2 = l2 != null ? l2.val : 0;

            ListNode l0 = new ListNode((num1 + num2 + carry) % 10);
            carry = (num1 + num2 + carry) / 10;
            cousor.next = l0;
            cousor = l0;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        return res.next;

    }

    public static void main(String[] args) {

        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);

        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(8);
        l1.next = l2;
        l2.next = new ListNode(5);
        ;
        l3.next = l4;
        ListNode l0 = addTwoNumbers(l1, l3);
        while (l0.next != null) {
            System.out.println(l0.val);
            l0 = l0.next;
        }
        System.out.println(l0.val);
    }
}


